import * as menuService from '@/services/menu';
import * as roleService from '@/services/role';
import * as rolePermissionService from '@/services/rolePermission';
import { message } from 'antd';
import { filter } from '@/utils/utils';
import { DEFAULT_RESPONE_SUCCESS } from '@/constants';

export default {
  namespace: 'role',

  state: {
    current: 1,
    total: 0,
    data: [],
    rowKey: 'roleId'
  },

  subscriptions: {

  },

  effects: {
    *queryData({ payload }, { call, put }) {
      const { callback, ...extra } = payload;

      const response = yield call(roleService.queryRole, extra);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;

        if (typeof callback === 'function') {
          callback(result);
        } else {
          if (payload.value) {
            yield put({
              type: 'filter', payload: {
                data: result,
                ...payload
              }
            });
          } else {
            yield put({
              type: 'changeState',
              payload: {
                data: result,
                current: payload.current,
                pageSize: payload.pageSize,
                total: Array.isArray(result) ? result.length : 0
              }
            });
          }
        }
      }
    },
    *filter({ payload }, { call, put }) {
      const { value, data, current, pageSize } = payload;

      if (value) {
        const newData = filter(data, value, ['roleCode', 'roleName']);
        yield put({
          type: 'changeState',
          payload: {
            current,
            filterData: newData,
            filterValue: value,
            pageSize,
            total: newData.length,
          }
        });
      } else {
        yield put({
          type: 'changeState',
          payload: {
            current,
            filterData: [],
            filterValue: '',
            pageSize,
            total: data.length,
          }
        });
      }
    },
    *saveOrUpdateData({ payload }, { call, put }) {
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: data
      };
      let response = {};
      if (!data.roleId) {
        response = yield call(roleService.saveRole, option);
      } else {
        response = yield call(roleService.updateRole, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('操作成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} }
        });
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *deleteData({ payload }, { call, put }) {
      const { roleId, ...extra } = payload;

      const response = yield call(roleService.deleteRole, { roleId });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('操作成功', 1);
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *queryMenu({ payload }, { call, put }) {
      const response = yield call(menuService.queryMenuTreeList, payload);
      if (response && Array.isArray(response.result)) {
        const data = response.result;

        yield put({
          type: 'changeState',
          payload: {
            menus: data
          }
        });
      }
    },
    *queryRolePermission({ payload }, { call, put }) {
      const response = yield call(rolePermissionService.queryRolePermission, payload);
      if (response && Array.isArray(response.result)) {
        const data = response.result;
        let rolePermission = [];
        if (Array.isArray(data) && data.length > 0) {
          rolePermission = data.map(item => item.resourceId);
        }
        yield put({
          type: 'changeState',
          payload: {
            rolePermission
          }
        });
        yield put({
          type: 'queryMenu',
        });
      }
    },
    *updateRolePermission({ payload }, { call, put }) {
      const { roleId, rolePermission } = payload;
      const option = {
        method: 'POST',
        body: rolePermission
      };
      const response = yield call(rolePermissionService.updateRolePermission, { roleId, option });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('操作成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: '', editData: {}, rolePermission: [] }
        });
      }
    }
  },

  reducers: {
    clear(state, { payload }) {
      return {
        current: 1,
        total: 0,
        data: [],
        rowKey: 'roleId'
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  },
};
